﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace SigmaT
{
    public partial class Form1 : Form
    {

        _Test _tes;
        int counter = 0;
        string _path = String.Empty;
        List<string> _charsS = new List<string>(new string[] { @"&amp;", @"&lt;", @"&gt;", @"&apos;", @"&quot;" });
        List<string> _charsC = new List<string>(new string[] { "&", "<", ">", "\'", "\"" });

        public Form1()
        {
            InitializeComponent();

            _tes = new _Test();
        }


        private string ReplaceAllOne(string inp)
        {

            if (inp == null) return "";

            if (inp == String.Empty) return "";

            if (inp.Length == 0) return "";

            string res = inp;

            for (int i = 0; i < _charsS.Count; i++)
            {
                res = res.Replace(_charsS[i], _charsC[i]);
            }

            return res;
        }

        private string ReplaceAllTwo(string inp)
        {
            if (inp == null) return "";

            if (inp == String.Empty) return ""; 

            if (inp.Length == 0) return "";
            
            string res = inp;

            for (int i = 0; i < _charsS.Count; i++)
            {
                res = res.Replace(_charsC[i], _charsS[i]);
            }

            return res;
        }
        private void LoadInFormQ()
        {
            txtU.Text = ReplaceAllOne(_tes.UText);
            txtS.Text = ReplaceAllOne(_tes.Text);

            txtQ.Text = ReplaceAllOne(_tes.questions[counter].Text);
            dataGridView1.Rows.Clear();

            foreach (var a in _tes.questions[counter].liAnswers)
            {

                dataGridView1.Rows.Add(new object[] { ReplaceAllOne(a.Text), a.Truth });
            }

            dataGridView1.Refresh();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button6_Click(sender, e);
        }


        private void button1_Click(object sender, EventArgs e)
        {

            counter = _tes.questions.Count;
            _tes.UText=txtU.Text;
            _tes.Text= txtS.Text;
            dataGridView1.Rows.Clear();
            dataGridView1.Refresh();
            txtQ.Text = String.Empty;

            _Question q = new _Question();

            q.Text = "Введите текст вопроса";

            _tes.questions.Add(q);

            LoadInFormQ();
        }



        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Application.StartupPath;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                XDocument xdoc = XDocument.Load(ofd.FileName);

                XElement xel = xdoc.Element("test");

                _tes = new _Test();
                _tes.UText = xel.Attribute("name").Value;
                _tes.Text = xel.Element("title").Value;

                foreach (var a in xel.Elements("question"))
                {
                    _Question q = new _Question();
                    q.Text = a.Element("text").Value;

                    foreach (var b in a.Elements("answer"))
                    {
                        bool ans = (b.Attribute("ttrue").Value == "1") ? true : false;
                        q.liAnswers.Add(new _Answer() { Text = b.Value, Truth = ans });
                    }

                    _tes.questions.Add(q);
                }

                counter = 0;
                LoadInFormQ();
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            _tes.UText = txtU.Text;
            _tes.Text = txtS.Text;
            _tes.questions[counter].Text = txtQ.Text;
            
            _tes.questions[counter].liAnswers = new List<_Answer>();
            
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value == null) return;

                if (dataGridView1.Rows[i].Cells[0].Value.ToString() == String.Empty) return;

                if (dataGridView1.Rows[i].Cells[0].Value.ToString().Replace(" ", "").Length == 0) return;

                _tes.questions[counter].liAnswers.Add(new _Answer()
                {
                    Text = dataGridView1.Rows[i].Cells[0].Value.ToString(),
                    Truth = (bool)(dataGridView1.Rows[i].Cells[1].Value??false)
                });
            }
        }

        private void ClearAll()
        {
            txtQ.Text = String.Empty;
            dataGridView1.Rows.Clear();
            dataGridView1.Refresh();
   
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (_tes.questions.Count <=1)
            {
                ClearAll();
                return;
            }

            _tes.questions.RemoveAt(counter);
            counter--;

            LoadInFormQ();

        }

        private void btnPred_Click(object sender, EventArgs e)
        {
            if (counter > 0)
            {
                counter--;
                
            }
            else
            {
                counter = 0;
            }
            LoadInFormQ();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (counter < _tes.questions.Count - 1)
            {
                counter++;
            }
            else
            {
                counter = _tes.questions.Count-1;
            }
            LoadInFormQ();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            _tes = new _Test();

            txtU.Text = "введите текст";
            txtS.Text = "введите текст";

            button1_Click(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button4_Click(sender, e);

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = Application.StartupPath;
            sfd.Filter = "(Xml Файлы)|*.xml";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                XDocument xdoc = new XDocument(new XDeclaration("1.0", "windows-1251", "yes"),
                    new XElement("test", new XAttribute("name", ReplaceAllTwo(_tes.UText)),
                        new XElement("title", ReplaceAllTwo(_tes.Text)),
                        _tes.questions.Select(x =>
                        new XElement("question", new XAttribute("type",
                            ((x.liAnswers.Where(y => y.Truth)).Count() > 1) ? "many" : "single"),
                            new XElement("text", ReplaceAllTwo(x.Text)),
                            x.liAnswers.Select(z => new XElement("answer", new XAttribute("ttrue",
                                (z.Truth) ? 1 : 0), ReplaceAllTwo(z.Text))))
                                ),
                         new XElement("valid", textBox1.Text),
                         new XElement("valid_2", textBox2.Text)));
                xdoc.Save(sfd.FileName);
            }       
        }
    }
}
